﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace SendToMe
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        List<string> _fileLinks;
        List<string> FileLinks
        {
            get
            {
                if (_fileLinks == null)
                    _fileLinks = new List<string>();
                return _fileLinks;
            }
        }

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtContent.Text))
            {
                this.Close(); return;
            }

            this.Hide();
            this.ShowInTaskbar = false;
            var content = txtContent.Text;

            Task.Run(() =>
            {
                try
                {
                    Mail.SendMail(content, FileLinks.ToArray());
                    this.Dispatcher.Invoke(Close);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    this.Dispatcher.Invoke(() =>
                    {
                        this.Show();
                        this.ShowInTaskbar = true;
                    });
                }
            });
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            txtContent.Focus();
        }

        private void txtContent_PreviewDrop(object sender, DragEventArgs e)
        {
            var @params = e.Data.GetData(DataFormats.FileDrop) as string[];
            if (_fileLinks == null)
            {
                var animation = FindResource("FileFirstAdding") as Storyboard;
                animation.Begin();
            }
            var adding = FindResource("FileAdding") as Storyboard;
            if (@params != null && @params.Length > 0)
            {
                foreach (var item in @params)
                {
                    if (!FileLinks.Contains(item))
                    {
                        FileLinks.Add(item);

                        Label label = new Label
                        {
                            Content = item.Substring(item.LastIndexOf("\\") + 1),
                            Background = new SolidColorBrush(Color.FromRgb(26, 156, 230)),
                            Width = 100,
                            VerticalContentAlignment = System.Windows.VerticalAlignment.Center,
                            Padding = new Thickness(5, 0, 5, 0),
                            BorderThickness = new Thickness(1)
                        };
                        sp_files.Children.Add(label);
                        Storyboard.SetTarget(adding, label);
                        adding.Begin();
                    }
                }
            }
        }

        private void txtContent_PreviewDragOver(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Handled = true;
                e.Effects = DragDropEffects.Link;
            }
            else
                e.Effects = DragDropEffects.None;
        }
    }
}
